package org.example.ch02.p07;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<String> strList = new ArrayList<>();

        boolean endFound = false;
        while (!endFound && scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();
            if (line.isEmpty()) continue;
            String[] parts = line.split("\\s+");
            for (String part : parts) {
                if (part.equalsIgnoreCase("!!end!!")) {
                    endFound = true;
                    break;
                }
                strList.add(part);
            }
        }

        strList.add(0, "begin");
        strList.add("end");
        System.out.println(strList);
        String str = scanner.nextLine().trim();
        System.out.println(strList.contains(str));
        System.out.println(strList.indexOf(str));
        System.out.println(strList.lastIndexOf(str));
        if (!strList.isEmpty()) {
            System.out.println(strList.remove(0));
        }
        System.out.println(strList);
        str = scanner.nextLine().trim();
        if (strList.size() >= 2) {
            strList.set(1, str);
        }
        System.out.println(strList);
        str = scanner.nextLine().trim();
        ArrayList<String> strList1 = new ArrayList<>();
        for (String s : strList) {
            if (s.contains(str)) {
                strList1.add(s);
            }
        }
        System.out.println(strList1);
        strList.remove(str);
        System.out.println(strList);
        strList.clear();
        System.out.println(strList + "," + strList.size() + "," + strList.isEmpty());
    }
}